<script lang="ts" setup>
import type { BaseinfoContractApi } from '#/api/csyt/baseinfocontract';

import { useVbenModal } from '@vben/common-ui';
import { message, Tabs, Checkbox, Input, Textarea, Select,RadioGroup,CheckboxGroup, DatePicker } from 'ant-design-vue';

import { computed, ref } from 'vue';
import { $t } from '#/locales';
import { useVbenForm } from '#/adapter/form';
import { getBaseinfoContract, createBaseinfoContract, updateBaseinfoContract } from '#/api/csyt/baseinfocontract';

import {useFormChat, useFormProduct, useFormSchema, xuqiu} from '../data';
import {useFormQuery} from "#/views/csyt/baseinfochat/data";
import {getBaseinfoChatInfo, deleteBaseinfoChatDelete, createBaseinfoChat, createBaseinfoChatadds,getBaseinfoChatLable} from '#/api/csyt/baseinfochat';
const emit = defineEmits(['success']);
const formData = ref<BaseinfoContractApi.BaseinfoContract>();
import {useRoute} from "vue-router";
const route = useRoute();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['合同信息'])
    : $t('ui.actionTitle.create', ['合同信息']);
});


const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  // layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
const [Form2, formApi2] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 80,
  },
  // 一共2列
  wrapperClass: 'grid-cols-2',
  layout: 'horizontal',
  schema: useFormChat(),
  showDefaultActions: false,
});
const [Form3, formApi3] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 80,
  },
  // 一共2列
  wrapperClass: 'grid-cols-2',
  layout: 'horizontal',
  schema: useFormProduct(),
  showDefaultActions: false,
});
const [Form4, formApi4] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 80,
  },
  // 一共2列
  wrapperClass: 'grid-cols-2',
  layout: 'horizontal',
  schema: xuqiu(),
  showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
        modalApi.lock();
    // 提交表单
    const data = (await formApi.getValues()) as BaseinfoContractApi.BaseinfoContract;
        try {
      await (formData.value?.id ? updateBaseinfoContract(data) : createBaseinfoContract(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success( $t('ui.actionMessage.operationSuccess') );
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    let data = modalApi.getData<BaseinfoContractApi.BaseinfoContract>();
    if (!data) {return;}
    let newInfo=await  getBaseinfoChatInfo({chatNo:route.query.chatNo});


    if (data.id) {
      modalApi.lock();
      try {
        data = await  getBaseinfoContract(data.id);
      } finally {
        modalApi.unlock();
      }
    }
    console.log(data.id)
    // 设置到 values
    formData.value = data;
    let form={
      chatId:newInfo.chatId,
      chatName:newInfo.chatName,
      chatNo:newInfo.chatNo,
      productCompany:newInfo.productCompany,
      demandCompany:newInfo.demandCompany
    }
    await formApi2.setValues(newInfo);
    await formApi3.setValues(newInfo.productInfo);
    await formApi4.setValues(newInfo.demandInfo);
    if(data.id){
      await formApi.setValues(formData.value);
    }else{
      await formApi.setValues(form);
    }

  },
});
</script>

<template>
  <Modal :title="getTitle">
<!--    <a-divider style="margin-top:0 !important;">会话信息</a-divider>-->
    <Form2 class="mx-4" />
<!--    <a-divider >产品信息</a-divider>-->
    <Form3 class="mx-4" />
<!--    <a-divider >需求信息</a-divider>-->
    <Form4 class="mx-4" />
<!--    <a-divider >合同信息</a-divider>-->
    <Form class="mx-4" />
  </Modal>
</template>
